package com.wu.flandre.api.page;

import com.wu.flandre.annotation.ApiCensus;
import com.wu.flandre.annotation.Authority;
import com.wu.flandre.annotation.ValidateAccessToken;
import com.wu.flandre.component.EmailComponent;
import com.wu.flandre.component.GlobalConstant;
import com.wu.flandre.entity.common.EmailEntity;
import com.wu.flandre.entity.common.ResponseTemp;
import com.wu.flandre.entity.page.Login;
import com.wu.flandre.service.UserAccountService;
import com.wu.flandre.service.UserInfoService;
import com.wu.flandre.util.HttpRequestUtil;
import com.wu.flandre.util.JwtUtil;
import com.wu.flandre.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * UserAccountApi <br/>
 *
 * @author 50816 <br/>
 * @date 2019/12/11 23:09 <br/>
 */
@RestController
@Slf4j
@Api(tags = "用户账号Api")
public class UserAccountApi {

    @Autowired
    private EmailComponent emailComponent;

    @Autowired
    private UserInfoService infoService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserAccountService accountService;

    @ApiCensus(apiName = "用户登录")
    @PostMapping("/userLogin")
    @ValidateAccessToken(value = false)
    public ResponseTemp userLogin(@RequestBody Login login) {
        return accountService.userLogin(login);
    }


    @GetMapping("/mail")
    @ApiCensus(apiName = "发送邮件")
    @ApiOperation(value = "发送邮件", notes = "发送邮件")
    public ResponseTemp testMail() {
        log.info("- 进入了testMail方法");
        EmailEntity entity = new EmailEntity();
        entity.setRecipient("1068093704@qq.com");
        entity.setSubject("账号注册");
        entity.setContent("账号注册");
        emailComponent.sendLoginMail(entity);
        return ResponseUtil.success("");
    }

    @GetMapping("/test")
    public ResponseTemp testSql() {
        String token = jwtUtil.generateToken("123456789", GlobalConstant.AUTH_NO);
        return ResponseUtil.success(token);
    }

    @GetMapping("/one")
    @ValidateAccessToken(authority = Authority.User)
    public ResponseTemp test2() {
        return ResponseUtil.success("需要User权限的Api");
    }

    @GetMapping("/two")
    @ValidateAccessToken(authority = Authority.No)
    public ResponseTemp test3() {
        return ResponseUtil.success("不需要权限的Api");
    }

    @GetMapping("/three/{name}")
    @ValidateAccessToken(value = false)
    public ResponseTemp test4(@PathVariable String name) {
        return ResponseUtil.success("无需认证的api，你的名字是：" + name);
    }


    @ApiCensus(apiName = "测试5")
    @GetMapping("/test5")
    @ValidateAccessToken(value = false)
    public ResponseTemp test5() {
        String get2String = HttpRequestUtil.getInstance().doGet2String("http://127.0.0.1:8081/three");
        System.out.println(get2String);
        return ResponseUtil.success("请求成功");
    }
}
